package com.yookey.config;

import com.google.common.collect.ImmutableMap;
import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.Iterator;

/**
 * className MessagePropertiesConfig
 *
 * @Author: Zhang Shao Hui
 * @Description:
 * @Date:Create：in 2019/11/20 13:38
 * @Modified By：
 */
@Configuration
public class MessagePropertiesConfig {
    private static ImmutableMap<String, String> propertiesMAP;

    private static void setPropertiesMAP(ImmutableMap<String, String> propertiesMAP) {
        MessagePropertiesConfig.propertiesMAP = propertiesMAP;
    }

    @Value("${message.paths}")
    private String paths;

    public static ImmutableMap<String, String> getPropertiesMap() {
        return MessagePropertiesConfig.propertiesMAP;
    }

    @PostConstruct
    public void init() throws ConfigurationException {
        CompositeConfiguration config = new CompositeConfiguration();
        String[] pathArray = this.paths.split(",");
        for (int i = 0; i < pathArray.length; i++) {
            config.addConfiguration(new PropertiesConfiguration(pathArray[i]));
        }

        ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<>();
        for (Iterator<String> iterator = config.getKeys(); iterator.hasNext();) {
            String key = (String) iterator.next();
            builder.put(key, config.getString(key));
        }
        setPropertiesMAP(builder.build());
    }
}
